<?php

namespace Keio\StudyBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\HttpFoundation\Response;

use Keio\StudyBundle\Entity\Package;
use Keio\StudyBundle\Entity\Packagebaihoc;
use Keio\StudyBundle\Entity\Baihoc;
use Keio\StudyBundle\Form\PackageType;

class PackageController extends Controller
{

	//show va add bài học vào package
	public function showAction($id){
		//lấy các bài học đã gắn vào package
		$em = $this->getDoctrine()->getManager();
		$package = $em->getRepository('KeioStudyBundle:Package')->find($id);

        $baihocIdY = $em->getRepository('KeioStudyBundle:Baihoc')->getBaihocIdByPackage(array($id));
        $baihocY = $em->getRepository('KeioStudyBundle:Baihoc')->getBaihocByGroupIdBaihoc($baihocIdY);

		//những bài học chưa gắn vào package
		$baihocN = $em->getRepository('KeioStudyBundle:Baihoc')->getBaihocExcludeSomeId($baihocIdY);
		//đẩy lên view
		return $this->render('KeioStudyBundle:Package:show.html.twig', array(
            'baihocY' => $baihocY,
			'baihocN' => $baihocN,
            'packageid'=> $id,
            'entity'   => $package,
        ));
	}

	//add baihoc to package
	//baihoc ID lay tu form submit
	public function addbaihoctopackageAction($package_id){
		$request = $this->getRequest();
		$array_baihoc = $request->request->get('baihoc');

		$em = $this->getDoctrine()->getManager();
		$package = $em->getRepository('KeioStudyBundle:Package')->find($package_id);

		for ($j=0; $j<count($array_baihoc); $j++){
			//nếu baihoc chưa đc submit từ form thì count này bằng 0 nên ko chạy ở đây
			$baihoc = $em->getRepository('KeioStudyBundle:Baihoc')->find($array_baihoc[$j]);
			$tmp = new Packagebaihoc();
			$tmp->setBaihoc($baihoc);
			$tmp->setPackage($package);
			$em->persist($tmp);
        	$em->flush();
		}
		return $this->redirect($this->generateUrl('keio_package_show', array('id'=>$package_id)));
	}

    /**
     * Lists all package entities.
     *
     */
     //Lấy các package lên hiển thị
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('KeioStudyBundle:Package')->findAll();
        //set delete form cho mỗi element
        foreach ($entities as $bh){
        	$deleteForm = $this->createDeleteForm($bh->getId());
        	$bh->setDelform($deleteForm->createView());
        }

        $entity = new Package();

        $form = $this->createFormBuilder($entity)
            ->add('name', 'text')
            ->add('description', 'text')
            ->getForm();

        return $this->render('KeioStudyBundle:Package:index.html.twig', array(
            'entities' => $entities,
            'form'   => $form->createView(),
        ));
    }

    public function createAction(Request $request)
    {
        $request = $this->getRequest();
		$data_form_name = $request->request->get('name');
		$data_form_dsc = $request->request->get('dsc');

        $em = $this->getDoctrine()->getManager();
        $entity  = new Package();
		$entity->setName($data_form_name);
		$entity->setDescription($data_form_dsc);
		$em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('keio_package'));
    }


    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('KeioStudyBundle:Package')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Package entity.');
        }

        $editForm = $this->createForm(new PackageType(), $entity);

        return $this->render('KeioStudyBundle:Package:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
        ));
    }

    /**
     * Edits an existing Baihoc entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('KeioStudyBundle:Package')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Package entity.');
        }

        $editForm = $this->createForm(new PackageType(), $entity);
        $editForm->bind($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('keio_package'));
        }

        return $this->render('KeioStudyBundle:Package:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
        ));
    }

    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('KeioStudyBundle:Package')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Package entity.');
            }

            //check if contraint
			$checkconstraint = $em->getRepository('KeioStudyBundle:Package')->checkConstraintToDeletePackage($id);
			if ($checkconstraint==1){ //having constraint
				$this->get('session')->getFlashBag()->add('msg', 'Delete failed because of constraint');
			}else{
				$em->remove($entity);
            	$em->flush();
			}
        }

        return $this->redirect($this->generateUrl('keio_package'));
    }

    /**
     * Creates a form to delete a entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
